package cn.wdm.basic.controller;

import cn.wdm.basic.dto.RegisterDto;
import cn.wdm.basic.exception.BusinesExceprion;
import cn.wdm.basic.service.IVerifyCodeService;
import cn.wdm.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService iVerifyCodeService;

    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult image(@PathVariable("imageCodeKey") String key){
        try {
            //将前端传的uuid传过去，返回一个base64编码的字符串。图片转为字符串。
            String imageCode=iVerifyCodeService.verifyCode(key);
            return AjaxResult.me().setResultObj(imageCode);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }

    }
    /**
     * 获取短信验证码接口
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult sendSms(@RequestBody RegisterDto dto){
        try {
            //获取短信验证码
            iVerifyCodeService.getSmsCode(dto);
            return AjaxResult.me();
        } catch (BusinesExceprion e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取短信验证码失败！"+e.getMessage());
        }
    }
    /**
     * 获取邮箱验证码接口
     * @return
     */
    @PostMapping("/getEmailCode")
    public AjaxResult getEmailCode(@RequestBody RegisterDto dto){
        try {
            //获取短信验证码
            iVerifyCodeService.getEmailCode(dto);
            return AjaxResult.me();
        } catch (BusinesExceprion e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取短信验证码失败！"+e.getMessage());
        }
    }


}
